<?php

namespace App\Http\Controllers\Api\AdminCode;

use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\Repositories\WxShopGoodsRepositories;
use App\Http\Controllers\Api\Repositories\WxSyncRelationRepositories;
use App\Models\Posts\WxPost;
use App\Models\Shop\WxShopGoods;
use Illuminate\Http\Request;

class AdminGoodsController extends BaseController
{
    /**
     * 增
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request){

        if(!is_array($request->tag) || !is_array($request->service_id)){
            return $this->fail(1001002, []);
        }
        if(_empty_($request->service_id)){
            $request->service_id = [];
        }

        // $external_id, $name, $pic, $tag,  $intro, $vip_price, $price,$content, $buys, $sku, $url, $classify_id, $service_id
        $r = WxShopGoodsRepositories::admin_add($request->domain, $request->id,  $request->name, $request->pic, $request->tag, $request->intro, $request->vip_price,
            $request->price, $request->post_content, $request->buys, $request->sku, $request->url, $request->classify_id, $request->service_id);
        if($r){
            return $this->success(['msg'=>'ok']);
        }else{
            return $this->fail(200002, ['msg'=>'商品数据插入失败']);
        }
    }

    /**
     * 删
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request){
        $inner_id = WxSyncRelationRepositories::wx_good_id($request->domain, $request->id);
        if(!$inner_id){
            return $this->fail(200003, ['msg'=>'没有对应的商品']);
        }
        if( WxShopGoods::where('id', $inner_id)->delete() ){
            return $this->success(['msg'=>'ok']);
        }else{
            return $this->fail(200002, ['msg'=>'商品数据插入失败']);
        }
    }

    /**
     * 改
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request){

    }

}
